package com.jtjrenren.android.taxi.driver.core.user.register;

import android.graphics.Bitmap;

import com.jtjrenren.android.taxi.driver.bean.api.CommonResult;
import com.jtjrenren.android.taxi.driver.bean.event.CompanyListEvent;
import com.jtjrenren.android.taxi.driver.bean.event.PhoneUniEvent;
import com.jtjrenren.android.taxi.driver.bean.event.RegisterEvent;
import com.jtjrenren.android.taxi.driver.bean.event.UpPicEvent;
import com.jtjrenren.android.taxi.driver.bean.event.YzmCheckEvent;
import com.jtjrenren.android.taxi.driver.bean.api.ApiCompanyList;
import com.jtjrenren.android.taxi.driver.bean.api.ApiResult;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;

import com.library.utils.Base64;

import org.greenrobot.eventbus.EventBus;

import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/8/19.
 */

public class RegisterInterctorImpl implements RegisterInterctor {
    @Override
    public void getCompanyList() {
        ApiWorks.getCompanyList("", new ApiWorks.ResponseListener<ApiCompanyList>() {
            @Override
            public void onResponse(ApiCompanyList apiCompanyList) {
                EventBus.getDefault().post(new CompanyListEvent(apiCompanyList));
            }
        });
    }

    @Override
    public void getYzm(String phone, String type) {
        ApiWorks.sendAuthCode(phone, type);
    }

    @Override
    public void checkPhoneUni(String phone) {
        ApiWorks.isUniquePhone(phone, new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                if (commonResult != null) {
                    if (commonResult.getCode() == 1)
                        EventBus.getDefault().post(new PhoneUniEvent(true, commonResult.getMsg()));
                    else
                        EventBus.getDefault().post(new PhoneUniEvent(false, commonResult.getMsg()));
                }
            }
        });
    }

    @Override
    public void checkYzm(String phone, String code, String type) {
        ApiWorks.checkYzm(phone, code,type , new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                if (commonResult != null && commonResult.getCode() == 1)
                    EventBus.getDefault().post(new YzmCheckEvent(true, ""));
                else
                    EventBus.getDefault().post(new YzmCheckEvent(false, "验证码验证失败"));
            }
        });
    }

    @Override
    public void upPic(final Bitmap bitmap) {
//        String imgBase64Str = "";
//        try {
//            ByteArrayOutputStream baos = new ByteArrayOutputStream();
//            bitmap.compress(Bitmap.CompressFormat.PNG, 60, baos);
//            imgBase64Str = URLEncoder.encode(Base64.encode(baos.toByteArray()), "utf-8");
//            baos.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        ApiWorks.upImg(imgBase64Str, new ApiWorks.ResponseListener<CommonResult>() {
//            @Override
//            public void onResponse(CommonResult commonResult) {
//                if (commonResult != null && commonResult.getCode() == 1)
//                    EventBus.getDefault().post(new UpPicEvent(true, commonResult.getData()));
//                else
//                    EventBus.getDefault().post(new UpPicEvent(false, "图片上传失败"));
//            }
//        });

        Observable.create(new Observable.OnSubscribe<String>() {

            @Override
            public void call(Subscriber<? super String> subscriber) {
                String imgBase64Str = "";
                try {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 60, baos);
                    imgBase64Str = URLEncoder.encode(Base64.encode(baos.toByteArray()), "utf-8");
                    baos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                subscriber.onNext(imgBase64Str);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        ApiWorks.upImg(s, new ApiWorks.ResponseListener<CommonResult>() {
                            @Override
                            public void onResponse(CommonResult commonResult) {
                                if (commonResult != null && commonResult.getCode() == 1)
                                    EventBus.getDefault().post(new UpPicEvent(true, commonResult.getData()));
                                else
                                    EventBus.getDefault().post(new UpPicEvent(false, "图片上传失败"));
                            }
                        });
                    }
                });
    }

    @Override
    public void register(String phone, String pwd, String companyID, String name, String yybh, String carcode, String sfzh, String iconUrl, String yyzUrl) {
        ApiWorks.driverRegister(phone, pwd, name, yybh, carcode, sfzh, iconUrl, yyzUrl, companyID, new ApiWorks.ResponseListener<ApiResult>() {
            @Override
            public void onResponse(ApiResult apiResult) {
                if (apiResult == null) {

                    return;
                }
                if (apiResult != null && apiResult.getResult().indexOf("US") < 0)
                    EventBus.getDefault().post(new RegisterEvent(true, "注册成功"));
                else if (apiResult != null && "US0021".equals(apiResult.getResult()))
                    EventBus.getDefault().post(new RegisterEvent(true, "账户信息审核中.."));
                else
                    EventBus.getDefault().post(new RegisterEvent(false, "注册失败"));
            }
        });
    }


}